<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="mainor_form">
          <div class="mainor_te">
            <!-- <el-button class="mainor_tea" @click="addedFormVisible = true"
              >新增批次</el-button
            > -->
            <el-button @click="exportbuVisible = true">导出</el-button>
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
          >
            <el-form-item label="商品" prop="goods_spu_id">
              <change-goods-input
                ref="changeGoodsInput"
                @clear="
                  () => {
                    this.form.goods_spu_id = '';
                  }
                "
                @sendValue="
                  data => {
                    this.form.goods_spu_id = data.id;
                  }
                "
              ></change-goods-input>
            </el-form-item>

            <el-form-item label="采购方式" prop="mode">
              <el-select
                v-model.trim="form.mode"
                placeholder="采购方式"
                class="wid220"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(modeList, index) in list.mode_list"
                  :key="index"
                  :label="modeList"
                  :value="parseInt(index + 1)"
                ></el-option>
              </el-select>
            </el-form-item>

            <div class="more el-form-item" @click="more">
              <span>更多</span>
              <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
            </div>

            <div class="clearfix"></div>

            <el-collapse-transition>
              <div v-if="moredata">
                <el-form-item label="批号" prop="batch_code">
                  <el-input
                    v-model.trim="form.batch_code"
                    placeholder="批号"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item>

                <el-form-item label="采购员" prop="buyer_id">
                  <el-select
                    v-model.trim="form.buyer_id"
                    placeholder="采购员"
                    class="wid220"
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(buyer, index) in list.buyer"
                      :key="index"
                      :label="buyer"
                      :value="parseInt(index + 1)"
                    ></el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="供应商" prop="supplier_id">
                  <el-select
                    v-model.trim="form.supplier_id"
                    placeholder="供应商"
                    class="wid220"
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(supplier, index) in list.supplier"
                      :key="index"
                      :label="supplier"
                      :value="parseInt(index + 1)"
                    ></el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="创建时间" prop="establishTime">
                  <el-date-picker
                    v-model="establishTime"
                    type="daterange"
                    class="time220"
                    :editable="false"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                    :clearable="false"
                    :picker-options="pickerOptions"
                  />
                </el-form-item>

                <div class="clearfix"></div>
              </div>
            </el-collapse-transition>

            <el-form-item class="marginTop10">
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="
                  $refs['userListFrom'].resetFields(),
                    (establishTime = []),
                    ($refs['changeGoodsInput'].name = '')
                "
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_table marginTop10" style="min-height: 550px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
            :header-cell-class-name="this.$verticaline"
          >
            <el-table-column type="selection" />

            <el-table-column label="批号" width="180px" prop="batch_code">
            </el-table-column>

            <el-table-column
              label="采购方式"
              prop="goods_code"
              min-width="120"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                <span :class="'type' + scope.row.mode">
                  {{ list.mode_list[scope.row.mode] }}</span
                >
              </template>
            </el-table-column>
            <el-table-column
              label="采购员/供应商"
              prop="market"
              min-width="120"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">{{
                scope.row.mode == 1
                  ? scope.row.buyer
                    ? scope.row.buyer.name
                    : ""
                  : scope.row.supplier
                  ? scope.row.supplier.name
                  : ""
              }}</template>
            </el-table-column>

            <el-table-column
              label="商品名称"
              prop=""
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                {{ scope.row.goods ? scope.row.goods.name : "" }}
              </template>
            </el-table-column>

            <el-table-column label="单位" prop="" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                {{
                  scope.row.goods_spu.spu_name
                    ? scope.row.goods_spu.spu_name.title
                    : ""
                }}
              </template>
            </el-table-column>

            <el-table-column label="本批库存" prop="storage_num">
            </el-table-column>

            <el-table-column label="剩余库存" prop="surplus_num">
            </el-table-column>

            <el-table-column label="创建时间" prop="created_at" width="150px">
              <template slot-scope="scope">
                <div>
                  {{
                    scope.row.created_at
                      ? parseTime(
                          scope.row.created_at,
                          "{y}-{m}-{d} {h}:{i}:{s}"
                        )
                      : ""
                  }}
                </div>
              </template>
            </el-table-column>

            <el-table-column
              label="管理操作"
              align="left"
              fixed="right"
              width="100"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <router-link :to="'/warehouse/batch/see/' + scope.row.id">
                  <el-link type="success">查看</el-link>
                </router-link>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!-- 新增批次 -->
        <!-- <el-dialog
          title="新增批次"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          @close="close"
          :visible.sync="addedFormVisible"
          class="main_dialog dialog600"
        >
          <el-form
            :inline="false"
            :model="info"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="120px"
          >
            <el-form-item label="商品" prop="goods_spu_id">
              <change-goods-input
                ref="changeGoodsInput"
                @clear="
                  () => {
                    this.info.goods_spu_id = '';
                  }
                "
                @sendValue="
                  data => {
                    this.info.goods_spu_id = data.id;
                  }
                "
              ></change-goods-input>
            </el-form-item>

            <el-form-item label="采购方式" prop="mode">
              <el-select
                v-model.trim="info.mode"
                placeholder="采购方式"
                class="wid220"
              >
                <el-option
                  v-for="(modeList, index) in list.mode_list"
                  :key="index"
                  :label="modeList"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="采购员" prop="buyer_id">
              <el-select
                v-model.trim="info.buyer_id"
                placeholder="采购员"
                class="wid220"
              >
                <el-option
                  v-for="(buyer, index) in list.buyer"
                  :key="index"
                  :label="buyer"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="供应商" prop="supplier_id">
              <el-select
                v-model.trim="info.supplier_id"
                placeholder="供应商"
                class="wid220"
              >
                <el-option
                  v-for="(supplier, index) in list.supplier"
                  :key="index"
                  :label="supplier"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="总库存：" prop="name">
              <el-input
                v-model.trim="info.name"
                placeholder="总库存"
                clearable
                class="wid300"
              ></el-input>
            </el-form-item>

          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="(addedFormVisible = false), close()"
              >取 消</el-button
            >
            <el-button type="primary" @click="setupmine" v-repeatClick
              >确 定</el-button
            >
          </div>
        </el-dialog> -->

        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>

        <!-- 订单导出 -->
        <el-dialog
          title="订单导出"
          :visible.sync="exportbuVisible"
          class="main_dialog dialog500"
        >
          <div class="selectTitle">已选择{{ selectArr.length }}条数据</div>
          <div slot="footer" class="dialog-footer">
            <el-button @click="exportbuVisible = false">取 消</el-button>
            <el-button type="primary" @click="exportConfirm" v-repeatClick
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { batch_index, batch_export } from "@/api/warehouse/warehouseBatch";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";
import { parseTime, exportMethod } from "@/utils/index";

export default {
  components: {},
  data() {
    return {
      // 时间
      parseTime,
      exportMethod,
      // 分页
      current: {
        page: 1,
        per_page: 20
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: []
      },
      listLoading: false,
      // 搜索
      form: {
        goods_spu_id: "",
        batch_code: "",
        start_time: "",
        end_time: "",
        mode: null,
        buyer_id: null,
        supplier_id: null
      },
      addedFormVisible: false,
      establishTime: [],
      pickerOptions: dateOption,
      // 更多
      moredata: false,
      // 全选
      selectArr: [],
      ids: [],
      exportbuVisible: false
      // info: {
      //   node: "",
      //   goods_spu_id: "",
      //   buyer_id: "",
      //   supplier_id: ""
      // }
    };
  },
  created() {
    // 列表
    this.batch_index();
  },
  methods: {
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map(item => {
        this.selectArr.push({ id: item.id });
      });
      console.log(this.selectArr);
    },

    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.batch_index();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.batch_index();
    },
    changePage(page) {
      this.current.page = page;
      this.batch_index();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 列表
    batch_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          )
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      batch_index(data).then(res => {
        console.log(res);
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.establishTime[0] ? this.establishTime[0] : "";
      form.end_time = this.establishTime[1] ? this.establishTime[1] : "";
      return form;
    },

    exportConfirm() {
      if (this.selectArr.length > 0) {
        let arr = [];
        this.selectArr.map(e => {
          arr.push(e.id);
        });
        this.ids = arr.join(",");
      }
      let forms = {
        ids: this.selectArr.length != 0 ? this.ids : ""
      };
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          )
        },
        apiUrl().name,
        forms
      );
           const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      batch_export(data).then(res => {
        console.log(res);
        this.exportMethod(res, "批次管理",notify);
      });
    }
  }
};
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 180px);
  }
}

// 支付状态
.success {
  color: $green;
}
.unpaid {
  color: $colour_red;
}
// 订单状态
.order0 {
  color: $order_color1;
}
.order1 {
  color: $order_color2;
}
.order2 {
  color: $order_color4;
}
</style>
